<?php
namespace app\api\model;

use think\Model;
use think\exception\ValidateException;

class SenderInfo extends Model
{
    protected $name = 'sender_info';
    protected $autoWriteTimestamp = true;

    /**
     * 新增发件人
     */
    public function senderCreate($param)
    {
        // 验证数据
        try {
            validate([
                'appid'  => 'require', 
                'sys_number'  => 'require', 
                'app_type'  => 'require', 
            ])->message([
                'appid.require'  => 'appid必须传递',   
                'sys_number.require'  => 'sys_number必须传递',   
                'app_type.require'  => 'app_type必须传递',   
            ])->check($param);
        } catch (ValidateException $e) {
            return result_error($e->getError());
        }

        $data['appid'] = $param['appid'] ?? '';//appid
        $data['third_number'] = $param['third_number'] ?? '';//第三方配送平台订单号
        $data['sys_number'] = $param['sys_number'] ?? '';//系统订单号
        $data['call_number'] = $param['call_number'] ?? '';//调用方订单号
        $data['app_type'] = $param['app_type'] ?? '';//第三方平台类型 1:闪送, 2:达达,3:蜂鸟
        $data['from_address'] =  $param['from_address'] ?? '';//发件地址
        $data['from_address_detail'] =  $param['from_address_detail'] ?? '';//发件详细地址
        $data['from_sender_name'] =  $param['from_sender_name'] ?? '';//发件人姓名
        $data['from_mobile'] =  $param['from_mobile'] ?? '';//发件联系人
        $data['from_latitude'] =  $param['from_latitude'] ?? '';//发件纬度（百度坐标系）
        $data['from_longitude'] =  $param['from_longitude'] ?? '';//发件经度（百度坐标系）

        // 创建
        try {
            $result = $this->save($data);

            if ($result) {
                return result_success('success',$result);
            } else {
                return result_error('保存失败');
            }
        } catch (\Exception $e) {
            return result_error($e->getMessage());
        }
    }

    /**
     * 更新发件人
     */
    public function senderUpdate($param)
    {
        if (empty($param['third_number'])&&empty($param['sys_number'])&&empty($param['call_number'])&&empty($param['id'])){
            return  result_error('缺少必要更新条件');
        }
        
        if(isset($param['from_address'])) $data['from_address'] = $param['from_address'];//发件地址
        if(isset($param['from_address_detail'])) $data['from_address_detail'] = $param['from_address_detail'];//发件详细地址
        if(isset($param['from_sender_name'])) $data['from_sender_name'] = $param['from_sender_name'];//发件人姓名
        if(isset($param['from_mobile'])) $data['from_mobile'] = $param['from_mobile'];//发件联系人
        if(isset($param['from_latitude'])) $data['from_latitude'] = $param['from_latitude'];//发件纬度（百度坐标系）
        if(isset($param['from_longitude'])) $data['from_longitude'] = $param['from_longitude'];//发件经度（百度坐标系）

        try {
           
            $find = $this
                    ->when(isset($param['third_number']),function ($query) use($param) {
                        $query->where('third_number', $param['third_number']);
                    })
                    ->when(isset($param['sys_number']),function ($query) use($param) {
                        $query->where('sys_number', $param['sys_number']);
                    })
                    ->when(isset($param['call_number']),function ($query) use($param) {
                        $query->where('call_number', $param['call_number']);
                    })
                    ->when(isset($param['id']),function ($query) use($param) {
                        $query->where('id', $param['id']);
                    })
                    ->find();
            if (!$find){
                 return  result_error('未查询到发件人');
            }  
            
            $result=$find->save($data);

            if ($result) {
               return result_success('success',$result);
            } else {
               return result_error('保存失败');
            }
        } catch (\Exception $e) {
             return result_error($e->getMessage());
        }
    }
}